﻿using Microsoft.EntityFrameworkCore;
using nhCore.db;
using nhLib.Extensions;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace envG;

public partial class Form数据库 : UIForm
{
    private readonly EnvDbContext _envDbContext = new();

    public Form数据库()
    {
        InitializeComponent();

        Ucbb表.SelectedIndexChanged += Ucbb表_SelectedIndexChanged;

        Udgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //标题居中
        Udgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; //自动列宽
        Udgv.AutoGenerateColumns = true;
        Udgv.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 9);
        Udgv.RowTemplate.DefaultCellStyle.Font = new Font("宋体", 9);

        UsbtnSave.Click += UsbtnSave_Click;
        下拉框初始化();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    }

    private void 下拉框初始化()
    {
        Ucbb表.Items.Add(nameof(_envDbContext.Stations));
        Ucbb表.Items.Add(nameof(_envDbContext.ElementConfigs));
        Ucbb表.Items.Add(nameof(_envDbContext.FSettings));
    }

    private async void Ucbb表_SelectedIndexChanged(object? sender, EventArgs e)
    {
        if (sender is not UIComboBox cbb) { return; }

        if (Udgv.DataSource is BindingSource bs)
        {
            foreach (var a in bs.List)
            {
                if (TEntityExtension.IsModifiedButNotSaved(_envDbContext, a))
                {
                    if (UIMessageBox.ShowMessageDialog("有数据未保存，现在保存吗？", "提示", true, Style))
                    {
                        UsbtnSave_Click(sender, new EventArgs());
                    }
                }
            }
        }

        switch (cbb.SelectedIndex)
        {
            case 0:
                await _envDbContext.Stations.LoadAsync();
                Udgv.DataSource = new BindingSource()
                {
                    DataSource = _envDbContext.Stations.Local.ToBindingList()
                };
                break;
            case 1:
                await _envDbContext.ElementConfigs.LoadAsync();
                Udgv.DataSource = new BindingSource()
                {
                    DataSource = _envDbContext.ElementConfigs.Local.ToBindingList()
                };
                break;
            case 2:
                await _envDbContext.FSettings.LoadAsync();
                Udgv.DataSource = new BindingSource()
                {
                    DataSource = _envDbContext.FSettings.Local.ToBindingList()
                };
                break;
        }
    }


    private void UsbtnSave_Click(object? sender, EventArgs e)
    {
        try
        {
            int ret = _envDbContext.SaveChanges();
            if (ret > 0)
            {
                //UIMessageBox.Show($"已更新{ret}条记录。", false, 2);
                UIMessageTip.ShowOk($"已更新{ret}条记录。");
            }
            else
            {
                UIMessageTip.ShowWarning($"没有数据更新。");
            }
        }
        catch (Exception ex)
        {
            UIMessageTip.ShowError(ex.Message);
        }
    }


}
